package com.lagou.edu.controller;

import com.lagou.edu.api.EmailService;
import com.lagou.edu.common.RestResult;
import com.lagou.edu.service.AuthCodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private AuthCodeService authCodeService;

    @Reference
    private EmailService emailService;

//    @Autowired
//    private RestTemplate restTemplate;

//    private boolean sendAuthCodeEmail(String email, String code) {
//        return emailService.sendMail(email, code);
////        String url = "http://lagou-service-email/email/" + email + "/" + code;
////        Boolean res = restTemplate.getForObject(url, Boolean.class);
////        return res;
//    }


    @RequestMapping("/create/{email}")
    public boolean create(@PathVariable String email) {

        String code = authCodeService.generateAuthCode(email);
        return emailService.sendMail(email, code);
    }

//    @RequestMapping("/validate/{email}/{code}")
//    public RestResult validate(@PathVariable String email, @PathVariable String code) {
//        int res = authCodeService.compareAuthCode(email, code); // 校验验证码. 0正确，1错误，2超时
//        RestResult restResult = new RestResult();
//        restResult.setStatus(res);
//        switch (res) {
//            case 0:
//                restResult.setMessage("验证成功！");
//                break;
//            case 1:
//                restResult.setMessage("验证失败！");
//                break;
//            case 2:
//                restResult.setMessage("验证码超时，请重新发送验证码！");
//                break;
//        }
//
//        return restResult;
//    }


}
